<?php
include_once('ConnectionManager.php');

function addReceipt($userId, $receiver, $address, $phone, $note, $code, $status) {
	$query = "INSERT INTO GIAAN_RECEIPT(USER_ID,RECEIPT_CODE,RECEIPT_DATE,RECEIPT_STATUS,RECEIPT_NOTE,RECEIPT_ADD,RECEIPT_PHONE,RECEIPT_RECEIVER) VALUES
			  ($userId, '$code', str_to_date(CURDATE(), '%Y-%m-%d'), '$status', '$note', '$address', '$phone', '$receiver')";
	ConnectionManager::getInstance()->setQuery($query);
	ConnectionManager::getInstance()->query();
	return mysql_insert_id();
}

function getReceiptCode($userId) {

	date_default_timezone_set('Asia/Ho_Chi_Minh');
	$code = date('dmy', time());
	
	$query = "SELECT COUNT(RECEIPT_CODE) FROM GIAAN_RECEIPT WHERE RECEIPT_DATE = str_to_date(CURDATE(), '%Y-%m-%d')";
	ConnectionManager::getInstance()->setQuery($query);
	$result = ConnectionManager::getInstance()->query();
	$row = mysql_fetch_array($result);
	
	$code .= $row[0] + 1;
	
	return $code;
}

function getLatestRecieptId() {
	$query = "SELECT MAX(RECEIPT_ID) AS MAX_ID FROM GIAAN_RECEIPT";
	ConnectionManager::getInstance()->setQuery($query);
	$resultSet = ConnectionManager::getInstance()->query();
	$result = mysql_fetch_array($resultSet);
	return $result['MAX_ID'];
	
}

function findAllReceiptsBy($keyword, $startPosition, $rowPerPage){
	$query = "SELECT R.*, (SELECT USER_NAME FROM GIAAN_USER U WHERE U.USER_ID = R.USER_ID) AS username FROM GIAAN_RECEIPT R ";
	$where = "";
	if(isset($keyword)) $where .= "WHERE R.RECEIPT_CODE LIKE '%".htmlspecialchars($keyword, ENT_QUOTES)."%'";			   
	$orderBy = " ORDER BY username asc LIMIT $startPosition, $rowPerPage";
//	echo $query.$where.$orderBy;
	ConnectionManager::getInstance()->setQuery($query.$where.$orderBy);
    return ConnectionManager::getInstance()->query();
}

function getAllReceiptOfUser($user_id){
    $query = "select RECEIPT_ID, RECEIPT_CODE, RECEIPT_DATE, RECEIPT_STATUS, RECEIPT_NOTE, RECEIPT_ADD, RECEIPT_PHONE, RECEIPT_RECEIVER
            from GIAAN_RECEIPT
            where USER_ID = $user_id";
    ConnectionManager::getInstance()->setQuery($query);
	return ConnectionManager::getInstance()->query();
}

function getAllProductOfReceipt($receipt_id){
    $query = "select PRO_ID, RECEIPT_ID, REI_QUANTITY, REI_PRICE
            from GIAAN_RECEIPTITEM r
            where RECEIPT_ID = $receipt_id";
    ConnectionManager::getInstance()->setQuery($query);
	return ConnectionManager::getInstance()->query();
}

function countReceiptBy($keyword){
	$query = "SELECT COUNT(RECEIPT_ID) AS NUM FROM GIAAN_RECEIPT WHERE RECEIPT_CODE LIKE '%".htmlspecialchars($keyword, ENT_QUOTES)."%'";
	ConnectionManager::getInstance()->setQuery($query);
	$row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
	return $row["NUM"];
}

function updateStatus($receiptId, $receiptType){
	$query = "UPDATE GIAAN_RECEIPT SET RECEIPT_STATUS = $receiptType WHERE RECEIPT_ID = $receiptId";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

function findReceiptBy($receiptId){
	$query = "SELECT * FROM GIAAN_RECEIPT WHERE RECEIPT_ID = $receiptId";
	ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function countAllReceipts(){
	$query = "SELECT COUNT(*) AS NUM FROM GIAAN_RECEIPT";
	ConnectionManager::getInstance()->setQuery($query);
	return ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
}
?>